package logic

import (
	"context"
	"errors"
	"novel-hub/models/mysql"
	"strings"

	"novel-hub/novelrpc/internal/svc"
	"novel-hub/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ContentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewContentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContentAddLogic {
	return &ContentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ContentAddLogic) ContentAdd(in *novelrpc.ContentAddRequest) (*novelrpc.ContentAddResponse, error) {
	// todo: add your logic here and delete this line

	content := mysql.Content{
		UserId:  in.UserId,
		NovelId: in.NovelId,
		Content: in.Content,
	}
	if strings.Contains(in.Content, "抄袭") || strings.Contains(in.Content, "涉政") {
		return nil, errors.New("评论内容违规")
	}
	if err := content.ContentAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &novelrpc.ContentAddResponse{
		ContentId: int64(content.ID),
	}, nil
}
